using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace SMS.Common.Xml
{
    public class Tools
    {
        ///<summary>
        /// Add an Element with tagName and value to specified XmlDocument and parent XmlElement.
        ///</summary>
        public static XmlElement AddElement(XmlDocument doc, XmlElement parent, string tag, string value)
        {
            try
            {
                XmlElement nodeElem = doc.CreateElement(tag);
                parent.AppendChild(nodeElem);
                if (!(value == null || value == string.Empty || value.Trim().Length == 0))
                {
                    XmlText nodeText = doc.CreateTextNode(value);
                    nodeElem.AppendChild(nodeText);
                }
                return nodeElem;
            }
            catch
            {
                return null;
            }
        }

        ///<summary>
        /// Add an Element with tagName to specified XmlDocument and parent XmlElement.
        ///</summary>
        public static XmlElement AddElement(XmlDocument doc, XmlElement parent, string tag)
        {
            return AddElement(doc, parent, tag, null);
        }

        ///<summary>
        /// Add an Element with tagName and value to specified XmlDocument and parent XmlNode.
        ///</summary>
        public static XmlElement AddElement(XmlDocument doc, XmlNode parent, string tag, string value)
        {
            try
            {
                XmlElement nodeElem = doc.CreateElement(tag);
                parent.AppendChild(nodeElem);
                if (!(value == null || value == string.Empty || value.Trim().Length == 0))
                {
                    XmlText nodeText = doc.CreateTextNode(value);
                    nodeElem.AppendChild(nodeText);
                }
                return nodeElem;
            }
            catch
            {
                return null;
            }
        }

        ///<summary>
        /// Add an Element with tagName to specified XmlDocument and parent XmlNode.
        ///</summary>
        public static XmlElement AddElement(XmlDocument doc, XmlNode parent, string tag)
        {
            return AddElement(doc, parent, tag, null);
        }

        ///<summary>
        /// Add an Attribute with name and value to a specified parent XmlElement.
        ///</summary>
        public static void AddAttribute(XmlElement parent, string name, string value)
        {
            try
            {
                parent.SetAttribute(name, value);
            }
            catch
            {
            }
        }

        ///<summary>
        /// Add an Attribute with name and value to a specified parent XmlElement.
        ///</summary>
        public static void AddAttribute(XmlNode parent, string name, string value)
        {
            try
            {
                if (parent.NodeType == XmlNodeType.Element)
                {
                    ((XmlElement)parent).SetAttribute(name, value);
                }
            }
            catch
            {
            }
        }

        ///<summary>
        /// Create and Save a new XmlFile, return XmlDocument object.
        ///</summary>
        public static XmlDocument CreateNewXmlDocument(string fileUri, string rootTagName)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0", "gb2312", ""));
            XmlElement xmlelem = xmldoc.CreateElement(rootTagName);
            xmldoc.AppendChild(xmlelem);

            try
            {
                FileInfo file = new FileInfo(fileUri);
                DirectoryInfo di = file.Directory;
                if (!di.Exists) di.Create();
                xmldoc.Save(fileUri);
                return xmldoc;
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
}
